// 封装storage.js

import config from "../config"

export default{
    setItem(key,val){
        // 设置缓存
        let storage=this.getStorage()
        storage[key]=val
        window.sessionStorage.setItem(config.namespaced,JSON.stringify(storage))
    },
    getItem(key){
        // 获取缓存
        return this.getStorage()[key]
    },
    clearItem(key){
        // 删除单项缓存
        let storage=this.getStorage()
        delete storage[key]
        window.sessionStorage.setItem(config.namespaced,JSON.stringify(storage))
    },
    clearAll(){
        // 清空缓存
        window.sessionStorage.clear()
    },
    getStorage(){
        // 区分缓存
        return JSON.parse(window.sessionStorage.getItem(config.namespaced)||'{}')
    }
}